//index.js
//获取应用实例
const app = getApp()
Page({

  data: { 
    indicatorDots: true,  //swiper
    autoplay: true,   //swiper
    interval: 5000,   //swiper
    duration: 1000,   //swiper
    indicatorColor: "#FFFFFF",  //swiper
    indicatorActiveColor: "#FFF",  //swiper
    
    userInfo: {},
    hasUserInfo: false,
    imageWidth: 0,
    imageHeight: 0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    focus:{}, //轮播图
    nav:{}, //导航
    superReward:{}, //超高反
    active:{
      img:"http://api.qm188.com/images/guide.png"
    }    //活动图
  },
  onLoad: function () {
    let that = this;
    wx.request({
      url: 'https://api.qm188.com/api/index',
      method: 'GET',
      success:function(r){
        console.log(r.data);
        let res = r.data;
        if(res.code === 200){
          let _data = res.data;
          that.setData({
            focus: _data.focus,
            nav:_data.nav,
            superReward: _data.super_reward
          })
        }
      }
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  imageLoad: function () {
    this.setData({
      imageWidth: wx.getSystemInfoSync().windowWidth,//图片宽度  

    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //图片自适应
  imgRespone: function (e) {
    //用来计算高宽  
    this.setData(wxAutoImageCal(e));
    function wxAutoImageCal(e) {
      //获取图片的原始长宽  
      var originalWidth = e.detail.width;
      var originalHeight = e.detail.height;
      var windowWidth = 0, windowHeight = 0;
      var imageWidth = 0, imageHeight = 0;
      var results = {};
      //获取屏幕信息  
      wx.getSystemInfo({
        success: function (res) {
          windowWidth = res.windowWidth;
          windowHeight = res.windowHeight;
          //判断按照那种方式进行缩放  
          if (originalWidth > windowWidth) {//在图片width大于手机屏幕width时候  
            imageWidth = windowWidth;
            imageHeight = (imageWidth * originalHeight) / originalWidth;
            results.imageWidth = imageWidth;
            results.imageHeight = imageHeight;
          } else {//否则展示原来的数据  
            results.imageWidth = originalWidth;
            results.imageHeight = originalHeight;
          }
        }
      })
      return results;
    }
  }, 
})
